package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.ParkCompanyDao;
import cn.topevery.garden.entity.po.Company;
import cn.topevery.garden.entity.query.CompanyQuery;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author code-generate
 * @date 2021-08-06
 */
@Repository
public class ParkCompanyDaoImpl extends BaseDaoImpl<Company> implements ParkCompanyDao {
    @Override
    public <P extends DbCsp> List<Company> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<Company> listPage(CompanyQuery query) {
        String table = "t_company tc left join t_section ts on ts.c_id=tc.c_section_id";
        String columns = "tc.*, ts.c_name sectionName";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tc.c_id desc");
        sqlQueryMate.where(true, "tc.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getTitle()), "tc.c_name", ScriptConditionEnum.Like, query.getTitle())
                .where(StringUtils.isNotBlank(query.getCode()), "tc.c_registration_code", ScriptConditionEnum.Like, query.getCode())
                .where(query.getSectionId() != null, "tc.c_section_id", ScriptConditionEnum.Eq, query.getSectionId());
        return getDataPager(sqlQueryMate, pageSize, pageIndex, Company.class);
    }

    /**
     * 描述 检查是否已经存在.
     *
     * @param fieldName 字段名
     * @param value     字段值
     * @return the boolean （true已存在，false不存在）
     * @author wenzhao.lin
     * @created 2021 -08-19 09:23:47 Is exit boolean.
     */
    @Override
    public boolean isExit(String fieldName, String value) {
        String table = "t_company";
        String columns = fieldName;
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, fieldName, ScriptConditionEnum.Eq, value)
        ;
        List<Company> list = selectList(sqlQueryMate, Company.class);
        if (list != null && list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<Map<String, Object>> getShortList(CompanyQuery query) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_company").setColumns("c_id id,c_company_id companyId,c_name name,c_section_id sectionId");
        sqlQueryMate.where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getSectionId()), "c_section_id", ScriptConditionEnum.Eq, query.getSectionId());
        return getMap(sqlQueryMate);
    }

}
